/*
 * RGB.c
 *
 *  Created on: 2025年7月4日
 *      Author: 黄少杰
 */
#include "RGB.h"

uint8_t ws_data[200]={0};
void ws2812_rgb(uint8_t ws_i,uint8_t ws_r,uint8_t ws_g,uint8_t ws_b)
{
    ws_data[(ws_i-1)*3]=ws_g;
    ws_data[(ws_i-1)*3+1]=ws_r;
    ws_data[(ws_i-1)*3+2]=ws_b;
}
void ws2812_rgb_all(uint8_t ws_i,uint8_t ws_r,uint8_t ws_g,uint8_t ws_b)
{
    static uint8_t rgb_wsi;
    for(rgb_wsi=1;rgb_wsi<=ws_i;rgb_wsi++)
    {
        ws_data[(rgb_wsi-1)*3]=ws_g;
        ws_data[(rgb_wsi-1)*3+1]=ws_r;
        ws_data[(rgb_wsi-1)*3+2]=ws_b;
    }
}

void ws2812_refresh(uint8_t ws_i)
{
    uint8_t ws_ri=0;
    for(;ws_ri<ws_i*3;ws_ri++)
    {
        if((ws_data[ws_ri]&0x80)==0)    send_0();   else send_1();
        if((ws_data[ws_ri]&0x40)==0)    send_0();   else send_1();
        if((ws_data[ws_ri]&0x20)==0)    send_0();   else send_1();
        if((ws_data[ws_ri]&0x10)==0)    send_0();   else send_1();
        if((ws_data[ws_ri]&0x08)==0)    send_0();   else send_1();
        if((ws_data[ws_ri]&0x04)==0)    send_0();   else send_1();
        if((ws_data[ws_ri]&0x02)==0)    send_0();   else send_1();
        if((ws_data[ws_ri]&0x01)==0)    send_0();   else send_1();
    }
    send_res();
}

void ws2812_reset(uint8_t ws_set)
{
    for(int i=0;i<200;i++)
    {
        ws_data[i]=ws_set;
    }
}
//
//void delay_250ns(void)
//{
//
//   // system_delay_10ns(23);
//}

void send_0(void)
{
    TH;
    TH;

    TL;
    TL;
    TL;
    TL;

}
void send_1(void)
{
    TH;
    TH;
    TH;


    TL;
    TL;
    TL;

}
void send_res(void)
{
    TL;
    R_BSP_SoftwareDelay(300,BSP_DELAY_UNITS_MICROSECONDS);//system_delay_us(300);
}


void RED()
{
    ws2812_rgb(1,20,0,0);
    ws2812_rgb(2,20,0,0);
    ws2812_rgb(3,20,0,0);
    //ws2812_rgb_all(3,0,20,0);
    ws2812_refresh(3);

}

void GREEN()
{
    ws2812_rgb(1,0,20,0);
    ws2812_rgb(2,0,20,0);
    ws2812_rgb(3,0,20,0);
    //ws2812_rgb_all(3,0,20,0);
    ws2812_refresh(3);

}

void YELLOW()
{
    ws2812_rgb(1,20,20,0);
    ws2812_rgb(2,20,20,0);
    ws2812_rgb(3,20,20,0);
    //ws2812_rgb_all(3,0,20,0);
    ws2812_refresh(3);

}

void bin()
{
    ws2812_rgb(1,0,20,20);
    ws2812_rgb(2,0,20,20);
    ws2812_rgb(3,0,20,20);
    //ws2812_rgb_all(3,0,20,0);
    ws2812_refresh(3);


}


uint8_t R = 0;
uint8_t G = 0;
uint8_t B = 0;

uint8_t region =0;
uint8_t hue =0;
void xuan()
{
    hue++;
    region = hue / 43;
    switch(region) {
        case 0:
            R = 255/10;
            G = (hue % 43 * 6)/10;
            B= 0;
            break;
        case 1:
            R = (255 - (hue % 43 * 6))/10;
            G = 255/10;
            B= 0;
            break;
        case 2:
            R = 0;
            G = 255/10;
            B= (hue % 43 * 6)/10;
            break;
        case 3:
            R = 0;
            G = (255 - (hue % 43 * 6))/10;
            B= 255/10;
            break;
        case 4:
            R = (hue % 43 * 6)/10;
            G = 0;
            B= 255/10;
            break;
        case 5:
            R = 255/10;
            G = 0;
            B= (255 - (hue % 43 * 6))/10;
            break;
        default:
            R = 255/10;
            G = 0;
            B= 0;
            break;
    }


    ws2812_rgb_all(36,R,G,B);
    ws2812_refresh(3);

}


void xuan_G()
{
    hue++;
    region = hue / 43;
    switch(region) {
        case 0:
            R = 255/10;
            G = (hue % 43 * 6)/10;
            B= 0;
            break;
        case 1:
            R = (255 - (hue % 43 * 6))/10;
            G = 255/10;
            B= 0;
            break;
        case 2:
            R = 0;
            G = 255/10;
            B= (hue % 43 * 6)/10;
            break;
        case 3:
            R = 0;
            G = (255 - (hue % 43 * 6))/10;
            B= 255/10;
            break;
        case 4:
            R = (hue % 43 * 6)/10;
            G = 0;
            B= 255/10;
            break;
        case 5:
            R = 255/10;
            G = 0;
            B= (255 - (hue % 43 * 6))/10;
            break;
        default:
            R = 255/10;
            G = 0;
            B= 0;
            break;
    }


            ws2812_rgb(1,R,G,B);
            ws2812_refresh(3);
            ws2812_rgb(2,B,R,G);
            ws2812_refresh(3);
            ws2812_rgb(3,G,R,B);
            ws2812_refresh(3);

}
